/**
 * Copyright(c)2012 Beijing PeaceMap Co.,Ltd.
 * All right reserved. 
 */
package com.pmc.dwa.security.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.pmc.dwa.common.dao.IGenericDao;
import com.pmc.dwa.common.utils.PaginationSupport;
import com.pmc.dwa.security.model.TGroup;
import com.pmc.dwa.security.model.TSect;
import com.pmc.dwa.security.model.TSection;
import com.pmc.dwa.security.service.ISectionResourceService;

/**
 * @description 单位/资源管理
 * @author aokunsang
 * @date 2013-1-7
 */
@Service("sectionResourceService")
public class SectionResourceServiceImpl implements ISectionResourceService {

	@Resource(name="genericDao")
	private IGenericDao genericDao;
	
	final static String addSyslog = "insert into t_sysoper_log values(:loginUname,:loginUid,:operTime,:operMod,:operEvent,:operFrom,:operPost,:operGet,:operIp,:operRemark)";
	final static String addSection = "insert into t_section values(:name,:tel,:intro,:status,:createTime,:version)";
	final static String editSection = "update t_section set NAME=:name,INTRO=:intro,CREATE_TIME=:createTime,TEL=:tel where ID=:id";
	final static String addSect = "insert into t_sect values(:pid,:group_id,:sectname,:comments,:remark,:slevel,:status,:sort,:icon,:updatetime,:version)";
	final static String editSect = "update t_sect set PID=:pid,GROUP_ID=:group_id,SECTNAME=:sectname,COMMENTS=:comments,REMARK=:remark,SLEVEL=:slevel,SORT=:sort,ICON=:icon where ID=:id";
	
	@Override
	public List<Map<String, Object>> findDistinctSectid(String strRoleIdlist) {
		return genericDao.queryMapList("select DISTINCT SECT_ID as sectid from T_ROLE_SECT t where ROLE_ID in("+strRoleIdlist+") order by SECT_ID asc");
	}
	@Override
	public List<TSect> findParentSect(String groupid,Integer slevel) {
		String orderBy = " order by PID";
		if(slevel == 1){
			orderBy = " order by SORT";
		}
		return genericDao.query("select * from t_sect where GROUP_ID = ? and SLEVEL = ? and STATUS = ?" + orderBy, TSect.class, new Object[]{groupid,slevel,"1"});
	}
	@Override
	public List<TSect> findSectListByParentidForUser(String groupid,String pid,
			String strSectIdlist) {
		return genericDao.query("select * from t_sect where ID in ("+strSectIdlist+") and GROUP_ID = ? and PID = ? and SLEVEL = 2 and STATUS = 1  order by SORT", TSect.class, new Object[]{groupid,pid});
	}
	@Override
	public List<TSection> getAllSection() {
		return genericDao.query("select * from t_section", TSection.class);
	}
	@Override
	public PaginationSupport<TSection> getPagesTSection(String queryname,
			int pageNumber, int pageSize) {
		String queryString = "select * from t_section where 1=1 ";
		if(StringUtils.isNotEmpty(queryname)){
			queryString += " and NAME like '%"+queryname+"%'";
		}
		return genericDao.queryListByRows(queryString + " order by CREATE_TIME desc", TSection.class, pageNumber, pageSize);
	}
	@Override
	public TSection getSectionByid(String secid) {
		return genericDao.find("select * from t_section where ID = ?", TSection.class, secid);
	}
	@Override
	public void addSection(TSection section) {
		genericDao.saveOrUpdate(addSection, section);
	}
	@Override
	public void editSection(TSection section) {
		genericDao.saveOrUpdate(editSection, section);
	}
	@Override
	public void editSectionColumnVal(String secid, String columnName,
			String columnVal) {
		genericDao.update("update t_section set "+columnName+" = ? where ID = ?", columnVal, secid);
	}
	@Override
	public PaginationSupport<TSect> getPagesTSect(String queryname,
			int pageNumber, int pageSize) {
		String queryString = "select * from t_sect where 1=1 ";
		if(StringUtils.isNotEmpty(queryname)){
			queryString += " and SECTNAME like '%"+queryname+"%'";
		}
		return genericDao.queryListByRows(queryString + " order by GROUP_ID asc,SLEVEL asc,SORT asc,UPDATETIME desc", TSect.class, pageNumber, pageSize);
	}
	@Override
	public TSect getSect(String sectid) {
		return genericDao.find("select * from t_sect where ID = ?", TSect.class, sectid);
	}
	@Override
	public void delSect(String sectid) {
		//先删除角色资源关联表，再删除资源表
		genericDao.update("delete from t_role_sect where SECT_ID = ?", sectid);  
		genericDao.update("delete from t_sect where ID = ?", sectid);
	}
	@Override
	public void addSect(TSect sect) {
		genericDao.saveOrUpdate(addSect, sect);
	}
	@Override
	public void editSect(TSect sect) {
		genericDao.saveOrUpdate(editSect, sect);
	}
	@Override
	public List<TGroup> getAllGroup() {
		return genericDao.query("select * from t_group", TGroup.class);
	}
	@Override
	public List<TSect> getAllTsect(String queryname) {
		String queryString = "select * from t_sect where 1=1 ";
		if(StringUtils.isNotEmpty(queryname)){
			queryString += " and SECTNAME like '%"+queryname+"%'";
		}
		return genericDao.query(queryString + " order by PID asc,SLEVEL asc,SORT asc,UPDATETIME desc", TSect.class);
	}
	@Override
	public void delSection(String sectionid) {
		genericDao.update("delete from t_section where ID = ?", sectionid);
	}
}
